<?php
namespace app\index\controller;

\think\Loader::import('controller/Jump', TRAIT_PATH, EXT);
use think\Controller;
use think\Db;
use think\Session;
use think\View;


class personal extends Controller
{
    public function personal(){
    	$after_time = strtotime(date('Ymd'));//当天零点
//  	$seven_next_time = strtotime(date('Ymd')) + 86400*7;//第二天零点
    	$user_id = Session::get('id')?Session::get('id'):0;
    	$timer = time();

		//查看访客之前先将create_time超过24小时的访客记录不显示
		$map['create_time'] = ['<',$after_time];
		Db::table('tp_look')->where($map)->setField('isdelete','1');
		//将超过7天的访客记录删除     创建时间+7天<time()
		$sql = "delete from tp_look where create_time < ".time()."-7*24*60*60";
		Db::query($sql);
		/*最近访客*/
		$field = "l.user_id,l.create_time,u.img";
		$visit = Db::table('tp_look')->alias('l')
				 ->join('tp_user u','l.user_id = u.id','left')
				 ->field($field)
				 ->where("l.fabu_id = $user_id and l.isdelete = 0")
				 ->whereTime('l.create_time','today')
				 ->order('l.create_time desc')
				 ->select();

		$this->view->assign('visit',$visit);
		//已策划2次活动/获得15个赞
		$active_count = Db::table("tp_info")->where(array('user_id'=>$user_id,'isdelete'=>0))->count();//活动次数
		$zan_count = Db::table("tp_info")->where(array('user_id'=>$user_id))->sum('zan');//点赞次数
		$this->view->assign("active_count",$active_count);
		$this->view->assign("zan_count",$zan_count?$zan_count:0);

		if($user_id){
	    	//个人资料
	    	$info = Db::table("tp_user")->where(array("id"=>$user_id))->find();
	    	//我的照片
			$myphoto = Db::table("tp_info")->field('photo,create_time')->where(array("user_id"=>$user_id,"isdelete"=>0))->order('create_time desc')->select();

			$my_photo_info = array();
			foreach($myphoto as $kk => $vv){
				$my_photo = $vv['photo'];
				$myphoto[$kk]['photo'] = explode("|",$my_photo);
				$myphoto[$kk]['create_time'] = date('Y/m/d H:i',$vv['create_time']);
			}
	    	//我的发布
	    	$fabu_info = array();
	    	
	    	$fabu_map['i.user_id'] = ['=',$user_id];
	    	$fabu_map['i.isdelete'] = ['=',0];

	    	$field = "i.*,c.cancel_status";
			$fabu_info = Db::table("tp_info")->alias('i')
				->join('tp_cancelinfo c','i.id = c.active_id','left')
				->field($field)
				->where($fabu_map)
				->order('i.create_time desc')
				->select();
//			var_dump(Db::table("tp_info"));exit;
			foreach($fabu_info as $k => $v){
				$fabu_info[$k]['comment_count'] = Db::table('tp_comment')->where(array('active_id'=>$v['id']))->count();
			}
			//我的关注
			$active = Db::table("tp_gz")->where(array("gz_id"=>$user_id,"isdelete"=>0))->select();
			$gz_data = array();
			
			foreach($active as $key => $val){
				$gz_info = Db::table("tp_info")->where(array("id"=>$val['info_id']))->find();
				if($gz_info){
					$gz_data[$key] = $gz_info;
				}
				$comment_count = Db::table('tp_comment')->where(array('active_id'=>$val['info_id']))->count();
				if($comment_count > 0){
					$gz_data[$key]['comment_count'] = $comment_count;
				}
			}
			//var_dump($gz_data);exit;
			$gz_photo_info = array();
			
			/*是否绑定手机号码*/
			$tel = Db::table("tp_user")->field('tel')->where(array('id'=>$user_id))->find();
			$is_tel = $tel['tel'];
			/*是否绑定邮箱*/
			$email = Db::table("tp_user")->field('email')->where(array('id'=>$user_id))->find();
			$is_email = $email['email'];
			/*是否绑定手机号码*/
			$real_name = Db::table("tp_user")->field('real_name')->where(array('id'=>$user_id))->find();
			$is_real_name = $real_name['real_name'];

			$myphoto_len = count($myphoto);
			$this->view->assign("myphoto_len",$myphoto_len);
			$this->view->assign("user_id",$user_id);
	    	$this->view->assign("info",$info);//我的资料
	    	$this->view->assign("my_photo_info",$myphoto);//我的照片
	    	$this->view->assign("fabu_info",$fabu_info);//发布信息
	    	$this->view->assign("gz_data",$gz_data);//我的关注
	    	$this->view->assign("gz_photo",$gz_photo_info);//我的关注图片信息
	    	$this->view->assign("is_tel",$is_tel);//判断是否已经绑定了手机号码
	    	$this->view->assign("is_email",$is_email);//判断是否已经绑定了邮箱
	    	$this->view->assign("is_real_name",$is_real_name);//判断是否已经进行实名认证
	    	return $this->view->fetch();
    	}else{
    		Session::set('url',$_SERVER['REQUEST_URI']);  
    		echo "<script>alert('您还未登录，请先登录！');window.location.href='/index/login/login_deng'</script>";
    	}
    }
    /*
	 * 上传图片
	 */
	public function mcupload(){
		if($_FILES['fb_img']['tmp_name']){
			$pic = $this ->request ->file('fb_img');
			$info = $pic -> move(ROOT_PATH . 'public/tmp/uploads/');

			if($info){
				$pic = '/tmp/uploads/'.$info->getSaveName();
				echo  $pic;exit;
			}else{
				echo "上传失败";exit;
			}

		}
	}
	
	/*
	 *修改背景 
	 */
	public function upback(){
		$img = $_POST['img'];
		$user_id = Session::get('id');
		
		$row = Db::table("tp_user")->where(array('id'=>$user_id))->setField('user_back',$img);
		if($row){
			echo "1";exit;
		}else{
			echo "-1";exit;
		}
	}
    public function personal_detail(){
    	$id = $_GET['id'];
    	
    	$user_info = Db::table("tp_user")->field("id")->where(array("id"=>$id))->find();
    	$user_id = $user_info['id'];

		//已策划2次活动/获得15个赞
		$active_count = Db::table("tp_info")->where(array('user_id'=>$user_id))->count();//活动次数
		$zan_count = Db::table("tp_info")->where(array('user_id'=>$user_id))->sum('zan');//点赞次数
		$this->view->assign("active_count",$active_count);
		$this->view->assign("zan_count",$zan_count);

        //个人资料
        $info = Db::table("tp_user")->where(array("id"=>$id))->find();
        //我的照片
        $myphoto = Db::table("tp_info")->field('photo,create_time')->where(array("user_id"=>$user_id))->select();

        $my_photo_info = array();
        foreach($myphoto as $kk => $vv){

            $my_photo = $vv['photo'];
            $myphoto[$kk]['photo'] = explode("|",$my_photo);
            $myphoto[$kk]['create_time'] = date('Y/m/d H:i',$vv['create_time']);
        }
        //我的发布
        $fabu_info = array();
        $fabu_info = Db::table("tp_info")
            ->where(array("user_id"=>$user_id,"isdelete"=>0))
            ->select();

        foreach($fabu_info as $k => $v){
            $fabu_info[$k]['comment_count'] = Db::table('tp_comment')->where(array('active_id'=>$v['id']))->count();
        }
        //最近访客
        $field = "l.user_id,l.create_time,u.img";
        $visit = Db::table('tp_look')->alias('l')
            ->join('tp_user u','l.user_id = u.id','left')
            ->field($field)
            ->where("l.fabu_id = $user_id and l.isdelete = 0")
            ->whereTime('l.create_time','today')
            ->order('l.create_time desc')
            ->select();
        $this->view->assign('visit',$visit);

        $this->view->assign('id',$id);
        $this->view->assign("user_id",$user_id);
        $this->view->assign("info",$info);//我的资料
        $this->view->assign("my_photo_info",$myphoto);//我的照片
        $this->view->assign("fabu_info",$fabu_info);//发布信息
        return $this->view->fetch();
    }
	/*
	 * 删除发布
	 */
	public function release(){
		$user_id = $_GET['user_id'];
		$id = $_GET['id'];
		$row = Db::table('tp_info')->where(array('user_id'=>$user_id,'id'=>$id))->update(array("isdelete"=>1));
		if($row){
			echo "<script>alert('删除发布成功！');window.location.href='/index/personal/personal?id={$user_id}'</script>";
		}else{
			echo "<script>alert('删除发布失败！');window.location.href='/index/personal/personal?id={$user_id}'</script>";
		}
	}

	/*
	 * 取消关注
	 */
	public function concern(){
		$gz_id = $_GET['uid'];
		$info_id = $_GET['info_id'];
		$row = Db::table('tp_gz')->where(array("gz_id"=>$gz_id,"info_id"=>$info_id))->setField('isdelete','1');
		if($row){
			echo "<script>alert('取消成功！');window.location.href='/index/personal/personal?id={$gz_id}'</script>";
		}else{
			echo "<script>alert('取消失败！');window.location.href='/index/personal/personal?id={$gz_id}'</script>";
		}
	}
	/*
    * 修改用户签名
     */
	public function updatesign(){
		$id = $_POST['id'];
		$data['sign'] = $_POST['sign'];
		$data['age'] = $_POST['age'];
		$data['name'] = $_POST['uname'];
		$data['sex'] = $_POST['sex'];
		Db::table('tp_user')->where(array("id"=>$id))->update($data);
	}
	/*
	 * 修改个人信息
	 */
	public function edit_info(){
		$id = $_GET['id'];
    	$user_info = Db::table("tp_user")->field("id")->where(array("id"=>$id))->find();
    	$user_id = $user_info['id'];

		if($user_id){
	    	//个人资料
	    	$info = Db::table("tp_user")->where(array("id"=>$id))->find();

			/*是否绑定手机号码*/
			$tel = Db::table("tp_user")->field('tel')->where(array('id'=>$user_id))->find();
			$is_tel = $tel['tel'];
			/*是否绑定邮箱*/
			$email = Db::table("tp_user")->field('email')->where(array('id'=>$user_id))->find();
			$is_email = $email['email'];
			/*是否绑定手机号码*/
			$real_name = Db::table("tp_user")->field('real_name')->where(array('id'=>$user_id))->find();
			$is_real_name = $real_name['real_name'];
			
			$this->view->assign("id",$id);
			$this->view->assign("user_id",$user_id);
	    	$this->view->assign("info",$info);//我的资料
	    	$this->view->assign("is_tel",$is_tel);//判断是否已经绑定了手机号码
	    	$this->view->assign("is_email",$is_email);//判断是否已经绑定了邮箱
	    	$this->view->assign("is_real_name",$is_real_name);//判断是否已经进行实名认证
	    	return $this->view->fetch();
    	}
		
		
	}
	/*
	 *查询个人信息 
	 */
	public function user_info(){
		$id = $_POST['id'];
		
		$user_info = Db::table('tp_user')->field('name,sex,age')->where(array('id'=>$id))->find();
		$info = [
			'code' => 1,
			'data' => $user_info
		];
		
		return json_encode($info);
	}
	/*
	 *个人信息修改昵称
	 */
	public function edit_uname(){
		$id = $_POST['id'];
		$data['name'] = $_POST['uname'];
		Db::table('tp_user')->where(array("id"=>$id))->update($data);
		$uname_info = Db::table('tp_user')->where(array("id"=>$id))->find();
		$uname = $uname_info['name'];
		return $uname;
	}
	/*
	 *个人信息修改性别 
	 */
	public function edit_sex(){
		$id = $_POST['id'];
		$data['sex'] = $_POST['sex'];
		$res = Db::table('tp_user')->where(array("id"=>$id))->update($data);
		if($res){
			$sex_info = Db::table('tp_user')->where(array("id"=>$id))->find();
			$sex = $sex_info['sex'];
			return $sex;
		}
	}
	/*
	 *个人信息修改年龄
	 */
	public function edit_age(){
		$id = $_POST['id'];
		$data['age'] = $_POST['age'];
		Db::table('tp_user')->where(array("id"=>$id))->update($data);
		$age_info = Db::table('tp_user')->where(array("id"=>$id))->find();
		$age = $age_info['age'];
		return $age;
	}
	public function edit_qianmin(){
		$id = $_POST['id'];
		$data['sign'] = $_POST['qian'];
		$res = Db::table('tp_user')->where(array("id"=>$id))->update($data);
		return $res;
	}
    /*
     * 钱包
     */
   	public function wallet(){
   		$id = $_GET['id'];
   		$money = Db::table("tp_user")->field("money")->where(array("id"=>$id))->find();
   		$money = sprintf("%.2f",$money['money']);
   		
   		$this->view->assign("id",$id);
   		$this->view->assign("money",$money);
    	return $this->view->fetch();
  	}
  	/*
  	 *钱包明细
  	 */
  	public function detail(){
  		$id = $_GET['id'];
		/*$cash_info = Db::table("tp_cash_notes")->alias("c")
					 ->join("tp_user u","c.friend_id = u.id","left")
					 ->where(array("c.user_id"=>$id,"c.isdelete"=>0))
					 ->order("c.create_time desc")
					 ->select();
		
		foreach($cash_info as $k => $v){
			$cash_info[$k]['create_time'] = date('Y年m月d日 H:i',$v['create_time']);
			$cash_info[$k]['money'] = sprintf("%.2f",$v['money']);
		}
		$this->view->assign("cash_info",$cash_info);*/
		$cash_info = Db::table('tp_balance')->where(array('balance_uid'=>$id,'isdelete'=>0))->select();

		foreach($cash_info as $k => $v){
			$cash_info[$k]['create_time'] = date('Y年m月d日 H:i',$v['create_time']);
			$cash_info[$k]['balance_quota'] = sprintf("%.2f",$v['balance_quota']);
		}
		$this->view->assign("cash_info",$cash_info);
		$this->view->assign("id",$id);
  		return $this->view->fetch();
  	}
  	/*
  	 *清除记录 
  	 */
  	public function cash_delete(){
  		$id = $_GET['id'];
  		$res = Db::table('tp_balance')->where(array("balance_uid"=>$id))->setField('isdelete', '1');
  		if($res){
  			echo "<script>alert('清除成功！');window.location.href='/index/personal/detail?id={$id}'</script>";
  		}else{
  			echo "<script>alert('操作失败！');window.location.href='/index/personal/detail?id={$id}'</script>";
  		}
  	}
  	/*
  	 *余额提现 
  	 */
  	public function cash(){
  		$id = $_GET['id'];
   		$money = Db::table("tp_user")->field("money")->where(array("id"=>$id))->find();
   		$money = sprintf("%.2f",$money['money']);
   		
   		$this->view->assign("id",$id);
   		$this->view->assign("money",$money);
  		return $this->view->fetch();
  	}
  	/*
	 *提现操作
	*/
	public function Withdraw(){
		$id = Session::get('id');
		$typer = $_POST['typer'];
		$num = $_POST['num'];
		if($typer == "1"){
			$arr['balance_con'] = "微信提现";
		}else{
			$arr['balance_con'] = "支付宝提现";
		}
		$arr['balance_describe'] = "提现";
		$arr['balance_quota'] = $num;
		$arr['create_time'] = time();
		$arr['balance_uid'] = $id;
		$res = Db::table("tp_balance")->insert($arr);
		if($res){
			$sql = "update tp_user set money = money - {$num} where id={$id}";
			$id=Db::execute($sql);
			if($id){
				echo "1";exit;
			}else{
				echo "-1";exit;
			}
		}
	}
  	/*
  	 *账户充值 
  	 */
  	public function racharge(){
  		return $this->view->fetch();
  	}
  	/*
  	 *我的参团 
  	 */
  	public function offered(){
  		$id = Session::get('id')?Session::get('id'):0;//当前登录用户id

  		$now_time = time();
  		$fields = "o.is_start,o.is_end,i.content,i.photo,i.id,i.user_id,o.button_status";
  		$info = Db::table("tp_offered")->alias('o')
		  		->join("tp_info i","o.active_id = i.id","left")
                ->field($fields)
		  		->where(array('o.user_id'=>$id,'o.isdelete'=>0))
				->order("i.create_time desc")
		  		->select();
		
		foreach($info as $k => $v){
            //地址显示处理
            $str=$v['address'];
            if(mb_strlen($str,'utf-8')>15){
                $info[$k]['address']=mb_substr($str,0,14,'utf-8').'...';
            }else{
                $info[$k]['address'] = $v['address'];
            }

			if($v){
				$info[$k]['comment_count'] = Db::table('tp_comment')->where(array('active_id'=>$v['id']))->count();
			}
			$info[$k]['count'] = $v['count']?$v['count']:0;
		}
		$this->view->assign('id',$id);//用户id
		$this->view->assign('info',$info);
		$this->view->assign('now_time',$now_time);
  		return $this->view->fetch();
  	}
  	/*
  	 *我的参团删除 
  	 */
  	public function offered_del(){
  		$id = $_GET['id'];//活动id
  		$uid = $_GET['uid'];//用户id
  		
  		$res = Db::table('tp_offered')->where(array("active_id"=>$id))->setField('isdelete', '1');
  		if($res){
  			echo "<script>alert('删除成功！');window.location.href='/index/personal/offered?id={$uid}'</script>";
  		}else{
  			echo "<script>alert('删除失败！');window.location.href='/index/personal/offered?id={$uid}'</script>";
  		}
  	}
  	/*
  	 *我的参团开始活动 
  	 */
  	public function edit_mark(){
  		$id = $_GET['active_id'];//活动id
  		$uid = $_GET['uid'];//用户id
  		$res = Db::table('tp_info')->where(array("id"=>$id))->setField('mark', '2');
  		if($res){
  			echo "<script>alert('操作成功！');window.location.href='/index/personal/offered?id={$uid}'</script>";
  		}
  	}
  	/**
  	 * 结束活动
  	 * */
    public function active_end(){
    	Db::startTrans();

    	$id = Session::get('id');//点击结束的人的id
        $active_id = $_POST['active_id'];

        $info = Db::table('tp_info')->where(array('id'=>$active_id))->find();
        $user_id = $info['user_id'];//活动发布者

        $deposit = $info['deposit'];//每个参与者应缴纳的押金
        $max_people = $info['max_people'];//允许参团的最大人数
        $fabu_money = $deposit*$max_people;//活动发布者所交押金

    	try{
    		$map['active_id'] = $active_id;
	        $map['user_id'] = $id;
	        $end['is_end'] = 1;
	        $end['button_status'] = 2;
    		Db::table('tp_offered')->where($map)->update($end);

    		$where['active_id'] = $active_id;
            $where['is_end'] = 1;

            //查询多少人参加某个活动
        	$offered_count = Db::table('tp_offered')->where('active_id',$active_id)->count();
        	//查询某个活动多少人点击了结束
            $end_count = Db::table('tp_offered')->where($where)->count();
            if($offered_count == $end_count) {
                //所有人都已经点了结束活动，将总的结束按钮is_end = 1
                Db::table('tp_info')->where('id',$active_id)->update(['is_end' => 1,'is_start' => 2]);

                //返还押金给发布者
                $bcredit = CREDIT;
                Db::table('tp_user')->where('id',$user_id)->setInc('money',$fabu_money);
                Db::table('tp_user')->where('id',$user_id)->setInc('Credit',$bcredit);

                //查询哪些人参加了活动
                $offered_user = Db::table('tp_offered')->where('active_id',$active_id)->select();
                $Credit = IN_CREDIT;
                foreach($offered_user as $k=>$v){
                    Db::table('tp_user')->where('id',$v['user_id'])->setInc('Credit',$Credit);
                }
            }
            $max['Credit'] = ['>',100];
            Db::table('tp_user')->where($max)->update(['Credit'=>100]);

            Db::commit();
            return json(['code' => 200, 'msg' =>'操作成功！']);
        } catch (\Exception $e) {
    		// 回滚事务
            Db::rollback();
            return json(['code' => 0, 'msg' => '操作失败！']);
    	}
    }
  	
	/*
	 * 用户实名认证
	 * */
	public function realname(){
		$uid = $_POST['uid'];
		$name = $_POST['name'];
		$IDcard = $_POST['IDcard'];
		$pic2 = $_POST['pic2'];
		
		$is_name = preg_match("/^([\xe4-\xe9][\x80-\xbf]{2}){2,4}$/",$name);
		$sfzh = preg_match("/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/",$IDcard);
		
		$data['id'] = $uid;
		$data['real_name'] = $name;
		$data['is_rz'] = 2;
		$data['IDcard'] = $IDcard;
		$data['card_photo2'] = $pic2;
		
		if($is_name){
			if($sfzh){
				if($pic2){
					$res = DB::table('tp_user')->update($data);
					if($res){
						echo '1';
//							echo "<script>alert('请求提交 ,审核时间大概两个工作日完成！');window.location.href='/index/personal/personal?id={$uid}'</script>";
					}else{
						echo '-4';
//							echo "<script>alert('请求认证失败，请查看数据是否填写完整！');window.location.href='/index/personal/personal?id={$uid}'</script>";
					}
				}else{
					echo '-3';exit;
				}
			}else{
				echo "-2";exit;
			}
			
		}else{
			echo "-1";exit;
		}
			
	}

	
	/*绑定手机号*/
	public function binding(){
		$user_id = $_POST['user_id'];
		$tel = $_POST['tel'];

		$send_code = Session::get("send_code");
		$sms_code = $_POST['sms_code'];

		if($send_code == $sms_code && $sms_code != null){
    		//验证码正确
    		
			//查询账号是否已经被绑定
			$is_binding = Db::table('tp_user')->where(array('tel'=>$tel))->count();
	    	
	    	if($is_binding == 0){
	    		$res = Db::table('tp_user')->where(array('id'=>$user_id))->setField('tel',$tel);
	    		if($res){
	    			echo "1";//绑定成功
	    		}
	    	}else{
	    		echo "-1";//该手机号码已被绑定！
	    	}
    	}else{
    		echo "-3";//验证码错误
    	}
	}
	/*绑定邮箱*/
	public function email(){
		$email = $_GET['email'];//用户邮箱
		$id = $_GET['user_id'];		
		$res = Db::table('tp_user')->where(array('id'=>$id))->setField('email',$email);
		if($res){
			echo "<script>alert('恭喜你！已经成功完成邮箱绑定！');window.close();</script>";
			
		}
	}
	/*取消活动*/
	public function cancel(){
		$active_id = $_POST['active_id'];//活动id
		$reason = $_POST['reason'];//取消活动原因
		
		
		$data = ['active_id' => $active_id, 'cancel_status' => '-1', 'reason' => $reason];
		$res = Db::table('tp_cancelinfo')->insert($data);
		if($res){
			return 1;
		}else{
			return -1;
		}
	}
	
	/*开始活动*/
	public function start(){
		$active_id = $_POST['active_id'];//活动id
		$data['is_start'] = 1;
		$data['create_time'] = time();
		
		$info = Db::table('tp_info')->field('go_time')->where('id',$active_id)->find();
		$go_time = $info['go_time'];
		
		if(time() > $go_time){
			$res = Db::table('tp_offered')->where('active_id',$active_id)->update($data);
		
			Db::table('tp_info')->where('id',$active_id)->setField('is_start',1);
			if($res){
				return 1;
			}else{
				return -1;
			}
		}else{
			//未到活动开始时间，操作失败
			return -2;
		}
	}
	//成员是否确认
	public function message(){
		$id = $_GET['id'];//活动id
		$time_now = time();
		$info = Db::table("tp_offered")->alias('o')
				->join("tp_user u","u.id = o.user_id","left")
				->join('tp_info i','o.active_id = i.id','left')
				->field("o.is_true,u.img,u.name,u.id as uid,o.button_status,i.end_time,i.id")
				->where(array("active_id"=>$id))
				->select();
		//var_dump($info);exit;
		
		$remind_info = Db::table('tp_remind')->where('active_id',$id)->select();
		
		
        $this->view->assign('info',$info);
        $this->view->assign('time_now',$time_now);
        $this->view->assign('remind_info',$remind_info);
		return $this->view->fetch();
	}
    //注销登录
    public function close(){
	    Session::delete('id');
        $this->success('注销登录成功', '/index/index/index');
    }
	        
	        //举报处理
	public function report(){
		$user_id = Session::get('id');
		$active_id = $_POST['active_id'];
		$data['report_con'] = $_POST['report_con'];
		$data['is_report'] = 1;
        $res = Db::table('tp_offered')->where(array('user_id'=>$user_id,'active_id'=>$active_id))->find();
		if($res){

			$row = Db::table('tp_offered')->where(array('user_id'=>$user_id,'active_id'=>$active_id))->update($data);
			if($row){
				//查询该活动所有成员是否举报
				$all = Db::table('tp_offered')->where(array('active_id'=>$active_id))->select();
				$cont = 0;
				foreach($all as $k=>$v){
					$a = $k;
					   if($v['is_report'] == "0"){
						    break;
					   }

					$cont++;//确认人数
				}
				$b = $a+1;//参与总人数
				if($cont == $b){
					//所有人举报，参与者返还押金,发放积分。
					    $deposit = Db::table('tp_info')->where(array('id'=>$active_id))->find();
					    $Credit = IN_CREDIT;
                        foreach($all as $m=>$s){
                                $db = "update tp_user set money = money + {$deposit['deposit']},Credit = Credit + {$Credit} where id = {$s['user_id']}";
							    Db::execute($db);
						}
                        $max['Credit'] = ['>',100];
                        Db::table('tp_user')->where($max)->update(['Credit'=>100]);

					    //发起者扣除积分,押金
					    $new = Db::table('tp_offered')->alias('o')
							->join('tp_info i','o.active_id = i.id','left')
					        ->field("i.user_id")
							->where(array('o.active_id'=>$active_id))
							->find();

					    $money = $cont*$deposit['deposit'];
					    $Credit1 = GO_DIE;
					    $sql = "update tp_user set money = money - {$money},Credit = Credit - {$Credit1} where id = {$new['user_id']}";
					    $good = Db::execute($sql);
					    if($good){
							echo "2";exit;
						}
					//提示发布者被举报

				}
				echo "1";exit;
			}else{
				echo "-1";exit;
			}
		}
	}
	/*
	 * 引导用户上传头像
	 * */
	public function photo(){
        $id = $_GET['id']?$_GET['id']:0;
        $info = Db::table('tp_user')->field('img')->where('id',$id)->find();
        $this->view->assign('id',$id);
        $this->view->assign('info',$info);
        return $this->view->fetch();
    }
    /*
     * 引导用户完善资料
     * */
    public function personal_info(){
        $id = $_GET['id']?$_GET['id']:0;
        $info = Db::table('tp_user')->field('name,sex,age')->where('id',$id)->find();
        $this->view->assign('info',$info);
        $this->view->assign('id',$id);
        return $this->view->fetch();
    }
    /*
     * 提交完善资料
     * */
	public function complete(){
        $id = $_POST['id'];
        $data['name'] = $_POST['name'];
        $data['sex'] = $_POST['sex'];
        $data['age'] = $_POST['age'];
        $data['tel'] = $_POST['tel'];
        $yzm = $_POST['yzm'];

        $s_yzm = session::get('info_yzm');

        if($s_yzm == $yzm){
            $res = Db::table('tp_user')->where('id',$id)->update($data);
            if($res){
                Session::set('id',$id);
                return json(['code' => 1, 'msg' => '个人信息修改成功！']);
            }else{
                return json(['code' => -2, 'msg' => '个人信息修改失败！']);
            }
        }else{
            return json(['code' => -1, 'msg' => '短信验证码错误，请重新输入！']);
        }
	}
    /*
     * 前往首页
     * */
    public function header_index(){
        $id = $_POST['id'];
        Session::set('id',$id);

        return 1;
    }
    /*
	 * 修改用户头像
	 */
    public function uphead(){
		$img = $_POST['img'];
		$user_id = $_POST['id'];
		$row = Db::table("tp_user")->where(array('id'=>$user_id))->setField('img',$img);
		if($row){
			return "1";
		}else{
			return Db::table("tp_user")->getLastSql();
		}
	}

	/*
	 * 用户签到
	 *
	 * 思路：关键在于如何判断是否是连续签到
	 * 在今天签到的数据插入tp_qiandao表之前，先将某用户上次签到的信息查询出来
	 * 当：昨日0点时间戳<=上次签到的时间（create_time）<=今日0点时间戳  时；说明昨日该用户签到了
	 * 如果该用户昨日签到：今日签到数据插入tp_qiandao表时，将连续签到天数（qiandao_ing）加1，今日签到鲜花数：10+2*($qiandao_ed['qiandao_ing']+1)
	 * 如果该用户昨日没签到：今日签到数据插入tp_qiandao表之前，将之前该用户的所以签到信息删除掉
	 * */
    public function qiandao(){
        $today_zero_time = strtotime(date('Ymd'));//今日0点的时间戳
        $after_zero_time = strtotime(date('Ymd'))-24*60*60;//昨日0点的时间戳
        $user_id = Session::get('id')?Session::get('id'):0;//用户id
        $qiandao_info = Db::table('tp_user')->where('id',$user_id)->find();
        $yi_qian = $qiandao_info['is_qiandao'];
        if($yi_qian == 1){
            return json(['code' => -2,'msg' => '今天已签到，请明天再来！']);
        }else{
            Db::table('tp_user')->where('id',$user_id)->update(array("is_qiandao"=>1));
            $qiandao_ed = Db::table('tp_qiandao')->where('user_id',$user_id)->order('create_time desc')->find();//查询上一次的签到记录
            if($qiandao_ed){
                if($qiandao_ed['create_time'] >= $after_zero_time && $qiandao_ed['create_time'] <= $today_zero_time){
                    //当上次签到的时间戳大于等于昨日的0点时间戳而且小于等于今日0点的时间戳时，说明该签到时间戳为昨日
                    //今日的签到鲜花数为：10+2*$qiandao_ed['qiandao_ing']+1
                    $flower_count = 10+2*($qiandao_ed['qiandao_ing']+1);
                    //今日为连续签到的第几日：$qiandao_ing = $qiandao_ed['qiandao_ing']+1
                    $qiandao_ing = $qiandao_ed['qiandao_ing']+1;
                }else{
                    $flower_count = 10;
                    $qiandao_ing = 1;
                    Db::table('tp_qiandao')->where('user_id',$user_id)->delete();
                }
            }else{
                $flower_count = 10;
                $qiandao_ing = 1;
            }
            $data['user_id'] = $user_id;
            $data['create_time'] = time();
            $data['flower_count'] = $flower_count;
            $data['qiandao_ing'] = $qiandao_ing;
            $result = Db::table('tp_qiandao')->insert($data);
            if($result){
                Db::table('tp_user')->where('id',$user_id)->setInc('flower',$flower_count);
                return json(['code' => 1,'msg' => '签到成功！']);
            }
        }
    }
    /**
     * 收送礼记录模板
     * */
    public function gift_notes(){
        $user_id = Session::get('id');

        $count = Db::table('tp_comment')->where(" (comment_id = $user_id or answer_id = $user_id) and type = 3 and isdelete = 0")->count();

        $this->view->assign('count',$count);
        return $this->view->fetch();
    }
    /**
     * 收送礼记录列表
     * */
    public function gift_notes_list(){
        $user_id = Session::get('id');
        $page = $_POST['page']?$_POST['page']:0;

        $limit = 10;
        $fields = "c.comment_id,c.answer_id,c.num,c.gift_type,c.create_time,u.name,u2.name named";
        $notes = Db::table('tp_comment')->alias('c')
            ->join('tp_user u','c.comment_id = u.id','left')
            ->join('tp_user u2','c.answer_id = u2.id','left')
            ->field($fields)
            ->where(" (comment_id = $user_id or answer_id = $user_id) and type = 3 and c.isdelete = 0 ")
            ->limit($page*$limit,$limit)->select();
        foreach($notes as $k => $v){
            $notes[$k]['create_time'] = date('m/d H:i',$v['create_time']);
        }

        $count = count($notes);

        $view = new View();
        $view->assign('notes',$notes);
        $view->assign('user_id',$user_id);
        $tpl = $view->fetch('gift_notes_tpl');

        return json(['code' => 200, 'tpl' => $tpl, 'count' => $count]);

    }
    /**
     * 拾友币充值页面
     * */
    public function coins_recharge(){
        $user_id = Session::get('id');

        $info = Db::table('tp_user')->where('id',$user_id)->find();

        $this->view->assign('info',$info);
        return $this->view->fetch();
    }
    /**
     * 充值拾友币
     * */
    public function recharge(){
        Db::startTrans();

        $user_id = Session::get('id');
        $price = $_POST['price'];//充值拾友币花费金额
        $coins = $price*10;//充值拾友币数量
        Db::table('tp_user')->where('id',$user_id)->find();

        try{
            Db::table('tp_user')->where('id',$user_id)->setDec('money',$price);
            Db::table('tp_user')->where('id',$user_id)->setInc('coins',$coins);
            Db::commit();
            return json(['code' => 1,'msg' => '充值成功 ']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['code' => -1,'msg' => '账户余额不足！', 'user_id' => $user_id]);
        }
    }
    /**
     * 清除收送礼记录
     * */
    public function delete_notes(){
        $user_id = Session::get('id');
        $res = Db::table('tp_comment')->where(" (comment_id = $user_id or answer_id = $user_id) and type = 3")->update(['isdelete'=>1]);
        if($res){
            return json(['code' => 200, 'msg' => '记录清除成功！']);
        }else{
            return json(['code' => -1, 'msg' => '记录清除失败！']);
        }
    }
}